﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using RaisingStudio.Data.Providers;
using RaisingStudio.Data.Providers.Adapters;
using RaisingStudio.Data.Providers.Adapters.Generic;

using RaisingStudio.Data.Expressions;
using RaisingStudio.Data.Entities;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            DataProvider dataProvider = CreateDataProvider();
            object maxIssueID = dataProvider.GetMaxData<Issue>(Issue._.IssueID);
            if ((maxIssueID != null) && (!(maxIssueID is DBNull)))
            {
                Program.IssueID_Conuter = Convert.ToInt32(maxIssueID);
            }
       }


        public virtual DataProvider CreateDataProvider()
        {
            DataProvider dataProvider = new DataProvider();
            dataProvider.Log = Console.Out;
            return dataProvider;
        }


        private void LoadIssueData()
        {
            DataProvider dataProvider = CreateDataProvider();
            this.dataGridView1.DataSource = dataProvider.GetDataTable(typeof(Issue));
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadIssueData();
        }

        private void NewIssue(object sender, EventArgs e)
        {
            Form2 form = new Form2();
            DialogResult dialogResult = form.ShowDialog();
            if (dialogResult == DialogResult.OK)
            {
                Issue issue = new Issue();
                // no autoincrement.
                issue.IssueID = ++Program.IssueID_Conuter;
                #region issue properties.
                issue.Title = form.IssueTitle;
                issue.Status = form.IssueStatus;
                if (!form.IsIssueTypeNull)
                {
                    issue.IssueType = form.IssueType;
                }
                issue.Owner = form.IssueOwner;
                if (!form.IsIssueProgressNull)
                {
                    issue.Progress = form.IssueProgress;
                }
                issue.CreateTime = System.DateTime.Now;
                if (!form.IsIssueLockedNull)
                {
                    issue.Locked = form.IsseuLocked;
                }
                issue.Description = form.IssueDescription;
                #endregion
                DataProvider dataProvider = CreateDataProvider();
                dataProvider.InsertDataObject<Issue>(issue);

                LoadIssueData();
            }
        }

        private void SaveIssues(object sender, EventArgs e)
        {
            DataProvider dataProvider = CreateDataProvider();
            dataProvider.Update(typeof(Issue), this.dataGridView1.DataSource as DataTable);
        }

        private void DeleteIssue(object sender, EventArgs e)
        {
            if ((this.dataGridView1.SelectedRows != null) && (this.dataGridView1.SelectedRows.Count > 1))
            {
                DialogResult dialogResut = MessageBox.Show("Are you sure to want delete these rows in the datatable?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResut == DialogResult.Yes)
                {
                    foreach (DataGridViewRow viewRow in this.dataGridView1.SelectedRows)
                    {
                        if (viewRow.DataBoundItem is DataRowView)
                        {
                            (viewRow.DataBoundItem as DataRowView).Row.Delete();
                        }
                    }
                }
            }
            else
            {
                int issueID = GetSeletedIssueID();
                if (issueID > 0)
                {
                    bool deleteDirectly = true;
                    // Popup messagebox.
                    DialogResult dialogResut = MessageBox.Show("Do you want delete it directly in the database?", "Delete", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                    if (dialogResut != DialogResult.Cancel)
                    {
                        if (dialogResut == DialogResult.No)
                        {
                            deleteDirectly = false;
                        }
                        if (deleteDirectly)
                        {
                            DataProvider dataProvider = CreateDataProvider();
                            dataProvider.DeleteByPrimaryKey<Issue>(issueID);

                            this.LoadIssueData();
                        }
                        else
                        {
                            DataTable dataTable = this.dataGridView1.DataSource as DataTable;
                            DataRow dataRow = dataTable.Rows.Find(issueID);
                            if (dataRow != null)
                            {
                                dataRow.Delete();
                            }
                        }
                    }
                }
            }
        }

        private int GetSeletedIssueID()
        {
            if (this.dataGridView1.CurrentRow != null)
            {
                DataRowView dataRowView = this.dataGridView1.CurrentRow.DataBoundItem as DataRowView;
                if (dataRowView != null)
                {
                    return (int)dataRowView.Row["IssueID"];
                }
            }
            return -1;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void RefreshList(object sender, EventArgs e)
        {
            this.LoadIssueData();
        }

        private void toolStripTextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                string searchText = this.toolStripTextBox1.Text;
                if (string.IsNullOrEmpty(searchText))
                {
                    this.LoadIssueData();
                }
                else
                {
                    DataProvider dataProvider = CreateDataProvider();
                    this.dataGridView1.DataSource = dataProvider.GetDataTable(typeof(Issue), Issue._.Title.Like(searchText, MatchOperator.Any) | Issue._.Description.Like(searchText, MatchOperator.Any));
                }
            }
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            int issueID = GetSeletedIssueID();
            if (issueID > 0)
            {
                DataProvider dataProvider = CreateDataProvider();
                dataProvider.Log = Console.Out;

                Issue issue = dataProvider.GetDataObjectByPrimaryKey<Issue>(issueID);

                Form2 form = new Form2();
                #region issue properties.
                form.IssueID = issue.IssueID;
                form.IssueTitle = issue.Title;
                form.IssueStatus = (IssueStatus)issue.Status;
                if (issue.IssueType != null)
                {
                    form.IssueType = (IssueType)issue.IssueType;
                }
                form.IssueOwner = issue.Owner;
                if (issue.Progress != null)
                {
                    form.IssueProgress = issue.Progress.Value;
                }
                form.CreateTime = issue.CreateTime;
                if (issue.Locked != null)
                {
                    form.IsseuLocked = issue.Locked.Value;
                }
                form.IssueDescription = issue.Description;
                #endregion
                form.Editing = true;
                DialogResult dialogResult = form.ShowDialog();
                if (dialogResult == DialogResult.OK)
                {
                    #region issue properties.
                    issue.Title = form.IssueTitle;
                    issue.Status = form.IssueStatus;
                    if (!form.IsIssueTypeNull)
                    {
                        issue.IssueType = form.IssueType;
                    }
                    else
                    {
                        issue.IssueType = null;
                    }
                    issue.Owner = form.IssueOwner;
                    if (!form.IsIssueProgressNull)
                    {
                        issue.Progress = form.IssueProgress;
                    }
                    else
                    {
                        issue.Progress = null;
                    }
                    issue.CreateTime = System.DateTime.Now;
                    if (!form.IsIssueLockedNull)
                    {
                        issue.Locked = form.IsseuLocked;
                    }
                    else
                    {
                        issue.Locked = null;
                    }
                    issue.Description = form.IssueDescription;
                    #endregion
                    dataProvider.UpdateDataObject<Issue>(issue);

                    LoadIssueData();
                }
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }
    }
}